package com.googlecode.spoluyan.google.googleapisdiscovery;

import java.util.HashMap;
import java.util.Map;

import com.googlecode.spoluyan.google.common.AbstractGoogleAPIClient;
import com.googlecode.spoluyan.google.common.Defines;
import com.googlecode.spoluyan.google.common.Defines.GoogleAPIsDiscoveryDefines.Label;
import com.googlecode.spoluyan.google.common.Utils;
import com.googlecode.spoluyan.google.googleapisdiscovery.data.DirectoryList;
import com.googlecode.spoluyan.google.googleapisdiscovery.data.DocumentResource;

public class GoogleAPIsDiscoveryClient extends AbstractGoogleAPIClient {

    public GoogleAPIsDiscoveryClient(String key) {
	super(key);
	this.name = Defines.ClientName.GOOGLE_APIS_DISCOVERY;
    }

    public DirectoryList getAPIsList(Label label, String name, Boolean preferred) {
	Map<String, String> queryParams = new HashMap<String, String>();
	if (label != null) {
	    queryParams.put(Defines.GoogleAPIsDiscoveryDefines.Parameter.LABEL, label.getName());
	}
	if (name != null) {
	    queryParams.put(Defines.GoogleAPIsDiscoveryDefines.Parameter.NAME, name);
	}
	if (preferred != null) {
	    queryParams.put(Defines.GoogleAPIsDiscoveryDefines.Parameter.PREFFERED,
		    String.valueOf(preferred.booleanValue()));
	}
	return Utils.fromJSON(Utils.doRequest(queryParams, Defines.GoogleAPIsDiscoveryDefines.API_URL),
		DirectoryList.class);
    }

    public DocumentResource getAPIInformation(String api, String version) {
	if (Utils.isEmptyString(api) || Utils.isEmptyString(version)) {
	    return null;
	}
	return Utils.fromJSON(Utils.doRequest(null, Defines.GoogleAPIsDiscoveryDefines.API_URL, Defines.SLASH, api,
		Defines.SLASH, version, Defines.SLASH, Defines.GoogleAPIsDiscoveryDefines.Path.REST),
		DocumentResource.class);
    }
}
